﻿@model KelonProud.Mvc.Models.DropDownSelectModel
@{
    var minimumResultsForSearch = Model.AllowSearch ? 0 : -1;
    var reloadFunction = Model.Name + "_Load";
}


<div class="input-group col-xs-12">
    <select id="@Model.Name" Name="@Model.Name" class="form-control " multiple="@Model.IsMultiple"></select></div>

<script>
    $(function() {
        @reloadFunction ('@Model.Url',"@Model.IsDisabled" === "True");
    });

    function @reloadFunction (url,isDisabled){

        abp.ajax({ url: url }).done(function(data) {
            var result = [];

            if ("@Model.AllowNull" === "True") {
                result.push({
                    "id": "@Model.NullItemValue",
                    "text": "@Html.Raw(Model.NullItemText)"
                });
            }

            for (var i = 0; i < data.length; i++) {
                result.push({
                    "id": data[i]["@Model.IdKey"],
                    "text": data[i]["@Model.TextKey"]
                });
            }

            try {
                $("#@Model.Name").select2("destroy");
                $("#@Model.Name").empty();
            }catch(ex){

            }

            var select = $("#@Model.Name").select2({
                minimumResultsForSearch: @minimumResultsForSearch,
                placeholder: "@Html.Raw(Model.Placeholder)",
                language: 'zh-CN',
                width: '100%',
                data: result,
                allowClear: "@Model.AllowNull" === "True"
            }).on('select2:select',
                function(e) {
                    var data = e.params.data;

                    if ("@Model.OnChangedEvent" !== "") {
                        @Model.OnChangedEvent (data);
                    }
                }).on('select2:selecting',
                function(e) {
                    var data = e.params.args.data;

                    if ("@Model.OnChangingEvent" !== "") {
                        var result = @Model.OnChangingEvent (data);

                        return result != false;
                    }
                });

            $("#@Model.Name").prop("disabled", isDisabled);

            if ("@Model.Value" !== "") {
                select.val("@Model.Value").trigger("change");
            }
        });
    }
</script>

